var dataProvider = require("../../provider/dataApi.js");

getApp();

Page({
    data: {
        statusBarHeight: "",
        titleBarHeight: "",
        canIUse: wx.canIUse("button.open-type.getUserInfo"),
        phoneNumber: "",
        verificationCode: "",
        codeText: "获取验证码",
        showPrivacy: 'no', // 隐私授权弹窗
    },
    onLoad: function (options) {

        this.setData({
            statusBarHeight: getApp().globalData.statusBarHeight,
            titleBarHeight: getApp().globalData.titleBarHeight
        });

    },
    // 关闭隐私弹框
    closePrivacy() {
        this.setData({
            showPrivacy: 'no'
        });
    },
    goBack: function () {
        wx.navigateBack({
            delta: 1
        });
    },
    getPhone: function (event) {
        this.setData({
            phoneNumber: event.detail.value
        });
    },
    getCode: function (event) {
        this.setData({
            verificationCode: event.detail.value
        });
    },
    sendCode: function () {
        var that = this;
        /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(this.data.phoneNumber) ? "获取验证码" == this.data.codeText || "重新发送" == this.data.codeText ? dataProvider.userinfo({
            phone: this.data.phoneNumber,
            op: "getcode"
        }).then(function (response) {
            that.setData({
                codeText: "60秒"
            });
            var seconds = 60,
                intervalId = setInterval(function () {
                    seconds--, that.setData({
                        codeText: seconds + "秒"
                    }), seconds <= 0 && (that.setData({
                        codeText: "重新发送"
                    }), clearInterval(intervalId));
                }, 1e3);
        }).catch(function (error) {
            console.log(error), wx.showToast({
                icon: "none",
                title: error.message
            });
        }) : wx.showToast({
            icon: "none",
            title: "请不要重复发送~"
        }) : wx.showToast({
            icon: "none",
            title: "请输入正确的手机号~"
        });
    },
    goLogin: function () {
        var phoneNumber = this.data.phoneNumber,
            verificationCode = this.data.verificationCode;
        11 != phoneNumber.length ? wx.showToast({
            title: "请输入正确的手机号",
            icon: "none"
        }) : "" == verificationCode ? wx.showToast({
            title: "请输入验证码",
            icon: "none"
        }) : wx.login({
            success: function (response) {
                var loginCode = response.code;
                console.log(loginCode), dataProvider.phoneLogin({
                    code: loginCode,
                    phone: phoneNumber,
                    sms_code: verificationCode
                }).then(function (response) {
                    console.log(response), wx.showToast({
                        title: response.message,
                        icon: "none"
                    }), 0 == response.errno && (wx.setStorageSync("uid", response.data.uid), setTimeout(function () {
                        wx.reLaunch({
                            url: "/pages/home/home"
                        });
                    }, 1500));
                }).catch(function (error) {
                    console.log(error), wx.showToast({
                        title: error.message,
                        icon: "none"
                    });
                });
            }
        });
    },

    getUserInfo2: function (event) {
        var that = this;
        if (wx.getPrivacySetting) {
            wx.getPrivacySetting({ //获取是否需要弹出
                success: response => {
                    console.log(response)
                    if (response.needAuthorization) {
                        wx.requirePrivacyAuthorize({ //该接口请求隐私协议，会自动触发上边的wx.onNeedPrivacyAuthorization
                            success: (response) => {
                                console.log(response)
                                that.getUserInfo3();
                            }
                        })
                    }
                }
            })
        }
        that.getUserInfo3();
    },



    getUserInfo3: function (event) {
        var that = this;
        wx.login({
            success: function (response) {
                var loginCode = response.code;
                dataProvider.login2({
                    code: loginCode,
                    nickName: "微信用户",
                    province: "北京市",
                    avatarUrl: "https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132",
                    city: "北京市",
                    is_band: 1
                }).then(function (response) {
                    wx.showToast({
                        title: response.message,
                        icon: "none"
                    }), wx.setStorageSync("uid", response.data.uid), dataProvider.userinfo({
                        uid: response.data.uid,
                        op: "getinfo"
                    }).then(function (response) {
                        console.log(response), wx.setStorageSync("userinfo", response.data.info), console.log(response.data.info.phone),
                            wx.reLaunch({
                                url: "/pages/home/home"
                            });
                    }).catch(function (error) {
                        console.log(error);
                    }), wx.hideLoading();
                }).catch(function (error) {
                    wx.hideLoading(), console.log(error), 1 == error.errno && wx.showToast({
                        icon: "none",
                        title: error.message
                    });
                }), that.userInfoReadyCallback && that.userInfoReadyCallback(response);
            }
        });
    },
    onShareAppMessage: function () {}
});